package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SysSupplier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysProduct;
import com.ruoyi.system.service.ISysProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品分类（二级）Controller
 *
 * @author ruoyi
 * @date 2024-07-16
 */
@RestController
@RequestMapping("/system/product")
public class SysProductController extends BaseController
{
    @Autowired
    private ISysProductService sysProductService;

    /**
     * 查询产品分类（二级）列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public AjaxResult list(SysProduct sysProduct)
    {
//        startPage();   //分页开关
        List<SysProduct> list = sysProductService.selectSysProductList(sysProduct);
        return success(list);
    }

    /**
     * 导出产品分类（二级）列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:export')")
    @Log(title = "产品分类（二级）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProduct sysProduct)
    {
        List<SysProduct> list = sysProductService.selectSysProductList(sysProduct);
        ExcelUtil<SysProduct> util = new ExcelUtil<SysProduct>(SysProduct.class);
        util.exportExcel(response, list, "产品分类（二级）数据");
    }

    /**
     * 获取产品分类（二级）详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping(value = "/{proId}")
    public AjaxResult getInfo(@PathVariable("proId") Long proId)
    {
        return success(sysProductService.selectSysProductByProId(proId));
    }

    /**
     * 获取产品的供应商列表
     */
    @GetMapping("/getSupplierList/{proId}")
    public AjaxResult getSupplierList(@PathVariable("proId") Long proId)
    {
        List<SysSupplier> list = sysProductService.selectSupplierListByProductId(proId);
        System.out.println(list);
        return success(list);
    }

    /**
     * 新增产品分类（二级）
     */
    @PreAuthorize("@ss.hasPermi('system:product:add')")
    @Log(title = "产品分类（二级）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProduct sysProduct)
    {
        return toAjax(sysProductService.insertSysProduct(sysProduct));
    }

    /**
     * 修改产品分类（二级）
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "产品分类（二级）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProduct sysProduct)
    {
        return toAjax(sysProductService.updateSysProduct(sysProduct));
    }

    /**
     * 删除产品分类（二级）
     */
    @PreAuthorize("@ss.hasPermi('system:product:remove')")
    @Log(title = "产品分类（二级）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{proIds}")
    public AjaxResult remove(@PathVariable Long[] proIds)
    {
        return toAjax(sysProductService.deleteSysProductByProIds(proIds));
    }
}
